package com.goldmantis.alitrip.controller.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.mongodb.MongoService;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.iterators.ObjectGraphIterator;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("mongo")
public class MongoController {

    @Autowired
    private MongoService mongoService;


    @GetMapping()
    public ResponseEntity<ReturnMessage> get() throws Exception {

//        MongoCollection<Document> collection = mongoService.getCollection("test", "assign_follow");
//
        List<Document> documents=new ArrayList<>();
//        MongoCursor<Document> iterator = collection.find().iterator();
//        while (iterator.hasNext()){
//            Document next = iterator.next();
//            documents.add(next);
//        }

//        MongoCursor<String> cursors = mongoService.getAllDataBaseNames().iterator();
//
//        while (cursors.hasNext()){
//            String next = cursors.next();
//            System.out.println(next);
//        }

//        MongoCursor<String> cursors = mongoService.getAllDataBaseNames().iterator();
//
//        while (cursors.hasNext()){
//            String next = cursors.next();
//            System.out.println(next);
//        }

        MongoDatabase wf_reminder = mongoService.getInstance().getDatabase("wf_reminder");
        Document document=new Document();
        document.put("d","12");
        wf_reminder.getCollection("wf_table").insertOne(document);
//

        MongoCursor<Document> iterator = wf_reminder.getCollection("wf_table").find(Filters.eq("d", "12")).iterator();

        while (iterator.hasNext()){
            documents.add(iterator.next());
        }

        return ResponseEntity.ok(ReturnMessage.success(documents));


    }
}
